package com.yuven.baselib.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.yuven.baselib.R;

public class AlertDialog extends Dialog {

    private Context mContext;
    private TextView tvContent;
    private TextView tvCancel;
    private TextView tvOK;

    private OnClickOkListener onClickOkListener;
    private OnClickCancelListener onClickCancelListener;

    private AlertDialog(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.core_dialog_options);
        getWindow().setBackgroundDrawable(new ColorDrawable());

        tvContent = findViewById(R.id.tvContent);
        tvCancel = findViewById(R.id.tvCancel);
        tvOK = findViewById(R.id.tvOK);
    }

    public void setContentText(String content) {
        tvContent.setText(content);
        ViewTreeObserver vto = tvContent.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                //单行居中 多行居左
                if (tvContent.getLineCount() == 1) {
                    tvContent.setGravity(Gravity.CENTER);
                } else {
                    tvContent.setGravity(Gravity.LEFT);
                }
                tvContent.getViewTreeObserver().removeOnPreDrawListener(this);
                return true;
            }
        });
    }

    public void setCancelText(String cancelText) {
        tvCancel.setText(cancelText);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onClickCancelListener != null) {
                    onClickCancelListener.onClickCancel();
                }
            }
        });
    }

    public void setDialogCanceledOnTouchOutside(boolean cancel) {
        setCanceledOnTouchOutside(cancel);
        setCancelable(cancel);
    }

    public void setCancelBtnVisible(boolean visible) {
        tvCancel.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setOkText(String okText) {
        tvOK.setText(okText);
        tvOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onClickOkListener != null) {
                    onClickOkListener.onClickOk();
                }
            }
        });
    }

    public void setOnClickOkListener(OnClickOkListener onClickOkListener) {
        this.onClickOkListener = onClickOkListener;
    }

    public void setOnClickCancelListener(OnClickCancelListener onClickCancelListener) {
        this.onClickCancelListener = onClickCancelListener;
    }

    public static class Builder {
        private String content;
        private String textCancel;
        private String textOk;

        private boolean cancelBtnVisible = true;   //取消按钮是否显示
        private boolean isCanceledOnTouchOutside = true;
        private OnClickOkListener okListener;
        private OnClickCancelListener cancelListener;

        private Context context;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setTextCancel(String textCancel) {
            this.textCancel = textCancel;
            return this;
        }

        public Builder setTextOk(String textOk) {
            this.textOk = textOk;
            return this;
        }

        public Builder setCancelBtnVisible(boolean cancelBtnVisible) {
            this.cancelBtnVisible = cancelBtnVisible;
            return this;
        }

        public Builder isCanceledOnTouchOutside(boolean cancel) {
            this.isCanceledOnTouchOutside = cancel;
            return this;
        }

        public Builder setOnClickOkListener(OnClickOkListener okListener) {
            this.okListener = okListener;
            return this;
        }

        public Builder setOnClickCancelListener(OnClickCancelListener cancelListener) {
            this.cancelListener = cancelListener;
            return this;
        }

        public AlertDialog create() {
            AlertDialog dialog = new AlertDialog(context);
            if (!TextUtils.isEmpty(content)) {
                dialog.setContentText(content);
            }
            if (!TextUtils.isEmpty(textCancel)) {
                dialog.setCancelText(textCancel);
            }
            if (!TextUtils.isEmpty(textOk)) {
                dialog.setOkText(textOk);
            }
            dialog.setDialogCanceledOnTouchOutside(isCanceledOnTouchOutside);
            dialog.setCancelBtnVisible(cancelBtnVisible);
            dialog.setOnClickOkListener(okListener);
            dialog.setOnClickCancelListener(cancelListener);
            return dialog;
        }
    }

    public interface OnClickOkListener {
        void onClickOk();
    }

    public interface OnClickCancelListener {
        void onClickCancel();
    }

}
